import { delLastComma, getForeachItemStr, leftStr2Arr, toCamelCase } from '@/utils/commonUtil.js'
import { commonConfig } from '@/utils/config.js'

export const columnToIn = (that) => {
  const { leftStr, splitArr } = leftStr2Arr(that.leftStr, that.separator)
  let resultStr = ''
  for (let item of splitArr) {
    resultStr += `'${ item }',\n`
  }
  that.rightStr = delLastComma(resultStr)
  addBracket(that)
  that.leftStr = leftStr
}
export const columnToForeach = (that) => {
  // that.leftStr = that.leftStr.replace('(', '').replace(')', '')
  // 分割字符串
  const { leftStr, splitArr } = leftStr2Arr(that.leftStr, that.separator)
  let resultStr = ''
  if (splitArr.length === 1) {
    resultStr = `${ commonConfig.outForeachPrefix }\t#{item}${ commonConfig.suffix }`
  } else {
    resultStr = `${ commonConfig.inForeachPrefix }(\n${ getForeachItemStr(splitArr) }\n)${ commonConfig.suffix }`
  }
  that.leftStr = leftStr
  that.rightStr = resultStr
}

/**
 * 添加括号
 * @param that
 */
function addBracket (that) {
  if (!that.rightStr.startsWith('(')) {
    that.rightStr = '(\n' + that.rightStr
  }
  if (!that.rightStr.endsWith(')')) {
    that.rightStr = that.rightStr + '\n)'
  }
}
